<?php
namespace data\set\cases;
use \data\BaseData;
/**
 * 案例管理
 */
class CaseGroupData extends BaseData{
    
    /**
     * 读取一个管理员信息
     */
    public function find($param){
        $check = ['id','plat','tag'];
        $where = $this->parseWhere($check,$param);
        if( !$where )  dataE('查询条件不能为空');
        $info  = M('set_case_group')->where($where)->find();
        return $info;
    }
    
    /**
     * 添加
     */
    public function add($data){               
        $check = [            
            'siteid'    => ['reg'=>'int'],
            'pid'       => ['def'=>0],
            'pic'       => '',
            'groupname'  => ['check'=>'must','msg'=> '请填写案例类目名称'],
            'depth'     => ['reg'=>[1,2,3]],
            'sort'      => ['def'=>50,'reg'=>'int'],
            'status'    => ['def'=>1]
        ];        
        $save = $this->verifyData($check,$data);
        $id = M('set_case_group')->insertGetId($save);
        if( !$id )  dataE('添加案例类目失败');
        return $id;
    }
    
    /**
     * 编辑
     */
    public function edit($param,$data){ 
        if( !$param ) dataE('修改参数不能为空');        
        $check = [            
            'siteid'    => ['reg'=>'int'],
            'pid'       => ['reg'=>'int'],
            'pic'       => '',
            'groupname' => ['reg'=>'must','msg'=> '请填写案例类目名称'],
            'depth'     => ['reg'=>[1,2,3]],
            'sort'      => ['reg'=>'int'],
            'status'    => ['reg'=>[1,2]]
        ];   
        $save = $this->verifyData($check,$data);
        if( !$save ) return false; 
        
        $wcheck = ['siteid','pid','id'];
        $where = $this->parseWhere($wcheck,$param);
        if( !$where ) return false;
        $result = M('set_case_group')->where($where)->update($save);
        if($result === false ) dataE('修改案例类目失败');
        return $result;
    }
    
    /**
     * 列表读取
     */
    public function getSimpleList($param){
        $page = $param['page']?:1;
        $limit= $param['limit']?:99;
        $check = ['siteid','status','groupname'=>'like','depth','pid'];
        $where = $this->parseWhere($check,$param);
        $list  = M('set_case_group')->where($where)->limit($limit)->page($page)->order('sort desc,id desc')->select();
        return $list;        
    }
}